package peer.gui;

import java.util.LinkedList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;


public class PeerListModel implements TableModel
{
	LinkedList<PeerModel> data = new LinkedList<PeerModel>();
	String[] columnNames = {"IP Address", "Port", "Connected", "% Complete", "Reqeusted block"};
	Class[] columnClasses = {String.class, Integer.class, Boolean.class, Double.class, Integer.class};
	
	TableModelListener listener;

	public void addTableModelListener(TableModelListener l)
	{
		listener = l;
	}

	public Class<?> getColumnClass(int columnIndex)
	{
		return columnClasses[columnIndex];
	}

	public int getColumnCount()
	{
		return columnNames.length;
	}

	public String getColumnName(int columnIndex)
	{
		return columnNames[columnIndex];
	}

	public int getRowCount()
	{
		return data.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex)
	{
		switch (columnIndex)
		{
		case 0:
			return data.get(rowIndex).address;
		case 1:
			return data.get(rowIndex).port;
		case 2:
			return data.get(rowIndex).connected;
		case 3:
			return data.get(rowIndex).percentCompleted;
		case 4:
			return data.get(rowIndex).gettingBlock;
		}
		return null;
	}

	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		return false;
	}

	public void removeTableModelListener(TableModelListener l)
	{
		listener = null;
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
	{
		
	}
	
	public class PeerModel
	{
		String address;
		int port;
		boolean connected;
		double percentCompleted;
		int gettingBlock;
		
		public PeerModel(String address, int port, boolean connected, double percentCompleted, int gettingBlock)
		{
			this.address = address;
			this.port = port;
			this.connected = connected;
			this.percentCompleted = percentCompleted;
			this.gettingBlock = gettingBlock;
		}
	}

	public void clear()
	{
		data.clear();
		listener.tableChanged(new TableModelEvent(this));
	}

	public void addPeer(String address, int port, double percentCompleted)
	{
		data.add(new PeerModel(address, port, false, percentCompleted, -1));
		listener.tableChanged(new TableModelEvent(this, data.size(), data.size(), TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
	}

	public boolean removePeer(String address)
	{
		boolean r = false;
		int i = 0;
		for (PeerModel peer : data)
		{
			if(peer.address.equals(address))
			{
				data.remove(); //hopefully this removes the correct one
				listener.tableChanged(new TableModelEvent(this, i));
				r = true;
			}
			i++;
		}
		return r;
	}

	public void updatePeerFileCompletion(String address, int port, double percentCompleted)
	{
		int i = 0;
		for (PeerModel peer : data)
		{
			if(peer.address.equals(address) && peer.port == port)
			{
				peer.percentCompleted = percentCompleted;
				listener.tableChanged(new TableModelEvent(this, i));
			}
			i++;
		}
	}

	public void updatePeerGettingBlock(String address, int port, int block)
	{
		int i = 0;
		for (PeerModel peer : data)
		{
			if(peer.address.equals(address) && peer.port == port)
			{
				peer.gettingBlock = block;
				listener.tableChanged(new TableModelEvent(this, i));
			}
			i++;
		}
	}

}
